package com.jinlejia.erp.ui.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityMessageBinding;
import com.netease.nimlib.sdk.NimIntent;
import com.nimlib.NimIMFragment;

import java.io.Serializable;

public class MessageActivity extends AppCompatActivity {

    private int segmentIndex = 0;

    public static void start(Context context){
        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra("channel","app");
        context.startActivity(intent);
    }

    private ActivityMessageBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_message);
        initView();

        Intent intent = getIntent();

        checkIntent(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        checkIntent(intent);
    }

    private void checkIntent(Intent intent){
        String channel = intent.getStringExtra("channel");
        if (TextUtils.equals("app",channel)){
            segmentIndex = 0;
        }else {
            segmentIndex = 1;
        }
        binding.segmentControl.setSelectedIndex(segmentIndex);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (segmentIndex == 0)
            ft.replace(R.id.content, new MessageListFragment());
        else if (segmentIndex == 1){
            ft.replace(R.id.content,getNimIMFragment(intent));
        }
        ft.commitAllowingStateLoss();
    }

    private NimIMFragment getNimIMFragment(Intent intent){
        Serializable extra = intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
        NimIMFragment nimIMFragment = new NimIMFragment();
        Bundle args = new Bundle();
        args.putSerializable("message",extra);
        nimIMFragment.setArguments(args);
        return nimIMFragment;
    }

    private void initView() {
        binding.segmentControl.setOnSegmentControlClickListener(index -> {
            hideSoftKeyboard();
            segmentIndex = index;
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            switch (index) {
                case 0:
                    ft.replace(R.id.content, new MessageListFragment());
                    ft.commitAllowingStateLoss();
                    break;
                case 1:
                    ft.replace(R.id.content, getNimIMFragment(getIntent()));
                    ft.commitAllowingStateLoss();
                    break;
                default:
                    break;
            }
        });
        findViewById(R.id.bt_back)
                .setOnClickListener(v -> finish());
    }

    protected void hideSoftKeyboard() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
                    0);
        }
    }
}
